<!--送审核-->
<template>
	<div class="need-submit-box pending-task"
       v-loading="loading"
       element-loading-customClass="ne-loading"
       element-loading-background="rgba(255, 255, 255, 0.6)">
		<h2 class="ne-title-v2">请选择审核单位</h2>
		<el-row style="width: 50%;margin: 0 auto;">
			<tc-form ref="tempForm" :form="Form1" :rules="rules" @submitFun="validate" @closeFun="closeFun"></tc-form>
		</el-row>
	</div>
</template>
<script>
import CustOrderService from "@/api/order/CustOrderService";
import TcForm from "@/components/public/TcForm";
import CommonUtil from "@/utils/common";
import { mapState } from "vuex";
import refreshList from "@/mixins/custorder/refreshList";
export default {
	name: "SendAudit",
  mixins: [refreshList],
	data() {
		return {
			loading: false,
      rules:{
        audCompany:[
          { required: true, message: '请选择审核单位', trigger: 'change' }
        ],
        audName:[
          { required: true, message: '请选择审核人', trigger: 'change' }
        ]
      },
			Form1: {
				//审核单位
				audCompany: {
					type: "select",
					cName: "审核单位",
					cValue: "",
					required: true,
					pair: "",
					opt: [],
					show: true,
					span: 24,
				},
				//审核人
				audName: {
					type: "select",
					cName: "审核人",
					cValue: "",
          filterable: true,
					required: true,
					opt: [],
					show: true,
					span: 24,
				},
			},
			audUserList: [],
		};
	},
	props: {
		ODKInfo: {
			type: Object,
			default() {
				return "";
			},
		},
		CTOInfo: {
			type: Object,
			default() {
				return "";
			},
		}
	},
	mounted() {},
	created() {
		this.gryDemandAudUserList();
	},
	components: {
		TcForm,
	},
	computed: {
		...mapState("user", ["user"]),
		...mapState("orders", ["orderPosInfo"]),
	},
	methods: {
    validate(){
      this.$refs['tempForm'].$refs['TcForm'].validate((valid) => {
        if (valid) {
          // 成功
          this.submitFun();
        } else {
          
          return false;
        }
      });
    },
		async gryDemandAudUserList() {
      // this.loading = true;
			let { orderAuditId, taskId, tacheId} = this.ODKInfo,
				{ custSoNumber, batchNbr, custOrderId,custId } = this.CTOInfo,
				params = { orderAuditId, taskId, tacheId, custSoNumber, batchNbr, custOrderId,custId };
			const result = await CustOrderService.gryDemandAudUserList(params);
			// this.loading = false;
			this.audUserList = this.handelUserListFun(result.meta.staffs.staff);
		},
		handelUserListFun(list) {
			let k = {},
				auditList = [];
			list.forEach((item) => {
				if (k[item.orgId]) {
					let { staffId, staffName } = item;
					auditList[k[item.orgId] - 1].staff.push({ value: staffId, label: staffName });
				} else {
					let { orgId, orgName, staffId, staffName } = item;
					this.Form1.audCompany.opt.push({ label: orgName, value: orgId });
					auditList.push({
						orgId: orgId,
						orgName: orgName,
						staff: [{ value: staffId, label: staffName }],
					});
					k[item.orgId] = auditList.length;
				}
			});
			return auditList;
		},
		closeFun() {
			this.$emit("update:show", false);
		},
		async submitFun() {
      this.loading = true;
			let nepara = CommonUtil.paramsFun2(this.Form1);
			nepara.audUserId = nepara.audName;
			this.Form1.audName.opt.forEach((item) => {
				if (item.value == nepara.audUserId) {
					nepara.audName = item.label;
				}
			});
			delete nepara["audCompany"];
			let { orderAuditId, taskId, tacheId, activityId, processInstId } = this.ODKInfo;
			let { custId, batchNbr, custSoNumber, custOrderId, orderItemId } = this.CTOInfo;
			let ldata = {
				orgId: this.user.orgId,
				staffId: this.user.staffId,
				staffName: this.user.staffName,
				// systemUserId:'',
				ssh: 0,
				checkOffer: 0,
				priority: 0,
				sfykj: 0,
				aZwbgAuditFlag: false,
				orderAuditUserFlag: false,
				oneOrmany: "many",
			};
			let allParam = { custId, orderItemId, activityId, processInstId, orderAuditId, taskId, tacheId, batchNbr, custSoNumber, custOrderId, ...ldata, ...nepara };
			let result = null;
			if (this.CTOInfo.batchNbr == "0") {
				// 需求单
				result = await CustOrderService.sendAuditDemandOrder(allParam);
			} else {
				// 订单
				result = await CustOrderService.sendAuditOrder(allParam);
			}
			this.loading = false;
			try {
				if (result.meta.resultCode == 0) {
					this.$message({
						type: "success",
						message: "送审成功!",
					});
          this.eventDoSearch();
					this.$emit("update:show", false);
					if (this.CTOInfo.cb) {
						this.CTOInfo.cb();
					}
					if(this.ODKInfo.ifTmp){
					  
            this.$attrs.getTempList;
          }
				} else {
					this.$message({
						showClose: true,
						message: result.meta.resultMsg,
						type: "error",
					});
				}
			} catch {
				this.$message({
					showClose: true,
					message: "code:" + result.code + "  msg:" + result.msg,
					type: "error",
				});
			}
		},
	},
	watch: {
		"Form1.audCompany.cValue": {
			handler(newName) {
				this.audUserList.forEach((item) => {
					if (item.orgId == newName) {
						this.Form1.audName.opt = item.staff;
					}
				});
			},
		},
	},
};
</script>

<style lang="scss" scoped>
.ne-title-v2 {
	margin-left: 0.12rem;
}
::v-deep .el-loading-spinner {
	i,
	.el-loading-text {
		color: rgba(0, 82, 217, 0.7);
	}
}
</style>
